package com.noodles.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.noodles.mall.entity.Role;
import com.noodles.mall.entity.UserRole;
import com.noodles.mall.mapper.UserRoleMapper;
import com.noodles.mall.service.RoleService;
import com.noodles.mall.service.UserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/** 用户角色 **/
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {
    /** 添加 **/
    public boolean insert(Integer userId,Integer roleId){
        UserRole userRole=new UserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(roleId);
        return super.save(userRole);
    }

    /** 查询列表: userId **/
    public List<Role> selectList(Integer userId){
        ArrayList<Role> list=new ArrayList<>();
        super.list(new QueryWrapper<UserRole>().lambda()
                .eq(UserRole::getUserId,userId)
        ).forEach(userRole -> list.add(RoleService.INTEGER_ROLE_MAP.get(userRole.getRoleId())));
        return list;
    }
}
